<!--
 * @Description: System Monitor Dashboard
 * @Author: Your Name
 * @Date: 2024-03-21
-->
<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { fetchGetSystemInfo } from '@/service/api';
import { NProgress } from 'naive-ui';

const systemInfo = ref<Api.SystemManage.ServerMonitorModel | null>(null);

async function getSystemInfo() {
  const { data } = await fetchGetSystemInfo();
  systemInfo.value = data;
}

onMounted(() => {
  getSystemInfo();
  // 定时刷新数据
  setInterval(getSystemInfo, 30000);
});
</script>

<template>
  <div class="system-monitor">
    <!-- 系统概览卡片 -->
    <div class="overview-cards">
      <div class="card cpu-card">
        <div class="card-header">
          <div class="header-left">
            <SvgIcon icon="flat-color-icons:electronics" class="text-24px mr-8px" />
            <h3>CPU 使用率</h3>
          </div>
          <div class="usage-circle">
            <NProgress
              type="circle"
              :percentage="Number(systemInfo?.cpu?.used) || 0"
              :color="Number(systemInfo?.cpu?.used) > 80 ? '#f56c6c' : '#67c23a'"
            />
          </div>
        </div>
        <div class="card-details">
          <div class="detail-item">
            <span>核心数</span>
            <span>{{ systemInfo?.cpu?.cpuNum || 0 }}</span>
          </div>
          <div class="detail-item">
            <span>系统使用率</span>
            <span>{{ systemInfo?.cpu?.sys || 0 }}%</span>
          </div>
          <div class="detail-item">
            <span>空闲率</span>
            <span>{{ systemInfo?.cpu?.free || 0 }}%</span>
          </div>
        </div>
      </div>

      <div class="card memory-card">
        <div class="card-header">
          <div class="header-left">
            <SvgIcon icon="flat-color-icons:sim-card" class="text-24px mr-8px" />
            <h3>内存使用率</h3>
          </div>
          <div class="usage-circle">
            <NProgress
              type="circle"
              :percentage="Number(systemInfo?.mem?.usage) || 0"
              :color="Number(systemInfo?.mem?.usage) > 80 ? '#f56c6c' : '#67c23a'"
            />
          </div>
        </div>
        <div class="card-details">
          <div class="detail-item">
            <span>总内存</span>
            <span>{{ systemInfo?.mem?.total || '0' }}</span>
          </div>
          <div class="detail-item">
            <span>已用内存</span>
            <span>{{ systemInfo?.mem?.used || '0' }}</span>
          </div>
          <div class="detail-item">
            <span>可用内存</span>
            <span>{{ systemInfo?.mem?.free || '0' }}</span>
          </div>
        </div>
      </div>

      <div class="card program-memory-card">
        <div class="card-header">
          <div class="header-left">
            <SvgIcon icon="flat-color-icons:self-service-kiosk" class="text-24px mr-8px" />
            <h3>程序内存占用</h3>
          </div>
          <div class="usage-circle">
            <NProgress
              type="circle"
              :percentage="Number(systemInfo?.py?.usage) || 0"
              :color="Number(systemInfo?.py?.usage) > 80 ? '#f56c6c' : '#67c23a'"
            />
          </div>
        </div>
        <div class="card-details">
          <div class="detail-item">
            <span>总内存</span>
            <span>{{ systemInfo?.py?.total || '0' }}</span>
          </div>
          <div class="detail-item">
            <span>已用内存</span>
            <span>{{ systemInfo?.py?.used || '0' }}</span>
          </div>
          <div class="detail-item">
            <span>可用内存</span>
            <span>{{ systemInfo?.py?.free || '0' }}</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 系统信息面板 -->
    <div class="info-panel">
      <div class="panel-section">
        <div class="header-left">
          <SvgIcon icon="fluent-emoji:desktop-computer" class="text-24px mr-8px" />
          <h3>系统信息</h3>
        </div>
        <div class="info-grid">
          <div class="info-item">
            <span class="label">服务器名称</span>
            <span class="value">{{ systemInfo?.sys?.computerName || '-' }}</span>
          </div>
          <div class="info-item">
            <span class="label">操作系统</span>
            <span class="value">{{ systemInfo?.sys?.osName || '-' }}</span>
          </div>
          <div class="info-item">
            <span class="label">服务器IP</span>
            <span class="value">{{ systemInfo?.sys?.computerIp || '-' }}</span>
          </div>
          <div class="info-item">
            <span class="label">系统架构</span>
            <span class="value">{{ systemInfo?.sys?.osArch || '-' }}</span>
          </div>
        </div>
      </div>
    </div>

    <!-- Python环境信息面板 -->
    <div class="info-panel">
      <div class="panel-section">
        <div class="header-left">
          <SvgIcon icon="devicon:python" class="text-24px mr-8px" />
          <h3>Python环境</h3>
        </div>
        <div class="info-grid">
          <div class="info-item">
            <span class="label">Python版本</span>
            <span class="value">{{ systemInfo?.py?.version || '-' }}</span>
          </div>
          <div class="info-item">
            <span class="label">启动时间</span>
            <span class="value">{{ systemInfo?.py?.startTime || '-' }}</span>
          </div>
          <div class="info-item">
            <span class="label">运行时长</span>
            <span class="value">{{ systemInfo?.py?.runTime || '-' }}</span>
          </div>
          <div class="info-item">
            <span class="label">安装目录</span>
            <span class="value">{{ systemInfo?.py?.home || '-' }}</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 磁盘使用情况 -->
    <div class="disk-panel">
      <div class="header-left">
        <SvgIcon icon="flat-color-icons:data-protection" class="text-24px mr-8px" />
        <h3>磁盘使用情况</h3>
      </div>
      <div class="disk-list">
        <div v-for="disk in systemInfo?.sysFiles" :key="disk?.dirName" class="disk-item">
          <div class="disk-header">
            <span class="disk-name">{{ disk?.dirName }}</span>
            <span class="disk-type">{{ disk?.typeName }}</span>
          </div>
          <div class="disk-progress">
            <NProgress
              :percentage="disk?.usage || 0"
              :color="disk?.usage > 80 ? '#f56c6c' : '#67c23a'"
            />
          </div>
          <div class="disk-details">
            <span>总空间: {{ disk?.total }}</span>
            <span>已用: {{ disk?.used }}</span>
            <span>可用: {{ disk?.free }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.system-monitor {
  padding: 20px;
  background: #f5f7fa;
  min-height: 100vh;
}

.overview-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
  margin-bottom: 20px;
}

.card {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.card-header h3,
.panel-section h3,
.disk-panel h3 {
  margin: 0;
  font-size: 18px;
  color: #303133;
  line-height: 1;
}

.card-details {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.detail-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.detail-item span:first-child {
  color: #909399;
  font-size: 14px;
  margin-bottom: 5px;
}

.detail-item span:last-child {
  color: #303133;
  font-size: 16px;
  font-weight: 500;
}

.info-panel {
  background: white;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.panel-section {
  margin-bottom: 30px;
}

.panel-section:last-child {
  margin-bottom: 0;
}

.panel-section .header-left {
  margin-bottom: 20px;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
}

.info-item {
  display: flex;
  flex-direction: column;
}

.info-item .label {
  color: #909399;
  font-size: 14px;
  margin-bottom: 5px;
}

.info-item .value {
  color: #303133;
  font-size: 16px;
}

.disk-panel {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.disk-panel .header-left {
  margin-bottom: 20px;
}

.disk-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
}

.disk-item {
  background: #f5f7fa;
  border-radius: 6px;
  padding: 15px;
}

.disk-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}

.disk-name {
  font-weight: 500;
  color: #303133;
}

.disk-type {
  color: #909399;
  font-size: 14px;
}

.disk-progress {
  margin: 10px 0;
}

.disk-details {
  display: flex;
  justify-content: space-between;
  font-size: 14px;
  color: #606266;
}

@media (max-width: 768px) {
  .overview-cards {
    grid-template-columns: 1fr;
  }
  
  .info-grid {
    grid-template-columns: 1fr;
  }
  
  .disk-list {
    grid-template-columns: 1fr;
  }
}
</style>
